package com.huaguoguo.cyg.spider.pipeline;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.huaguoguo.cyg.entity.weibo.to.WBDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 类说明： WeiboUserProcessor爬到的数据进行处理<br/>
 *  保存到文件中，一条微博一个 {mid}.json 文件
 * author:huaguoguo
 * Date: 2019/8/16
 * Time: 23:15
 */
@Component
@Slf4j
public class WeiboUserFilePipeline implements Pipeline {

    /**
     * 处理爬虫数据.写入到文件中
     *
     * @param resultItems resultItems
     * @param task        task
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        List<WBDetail> wbDetailList = (List<WBDetail>) resultItems.get("wbList");
        // 爬虫数据对象 转 json
        log.info("待写入文件数量：{}",wbDetailList.size());
        wbDetailList.forEach(wbDetail -> {
            String jsonStr = JSONUtil.toJsonPrettyStr(wbDetail);
            String filePath = StrUtil.concat(true,"C:\\TEMP\\weibo\\blog\\",wbDetail.getMid(),".json");
            FileUtil.writeString(jsonStr, filePath, StandardCharsets.UTF_8);
            log.info("文件{}{}写入完成",wbDetail.getMid(),".json");
        });
    }

}
